package com.fg.ihr.controller;

import com.fg.ihr.domain.PublicMsg;
import com.fg.ihr.domain.Ueditor;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.slf4j.Logger;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
public class UEdIitorController {
    private final static Logger logger = LoggerFactory.getLogger(UEdIitorController.class);

//    @Value("${file.uploadFolder}")
//    private String uploadfolder;

    @GetMapping("/baidu")
    private String home() {
        return "index";
    }

    @RequestMapping(value = "/ueditor")
    @ResponseBody
    public String ueditor(HttpServletRequest request) {
        return PublicMsg.UEDITOR_CONFIG;
    }


    @RequestMapping(value = "/imgUpload")
    @ResponseBody
    public Ueditor imgUpload(HttpServletRequest req) {
        Ueditor ueditor = new Ueditor();
        MultipartHttpServletRequest mReq = null;
        MultipartFile file = null;
        String fileName = "";
        // 原始文件名 UEDITOR创建页面元素时的alt和title属性
        String originalFileName = "";
        try {
            mReq = (MultipartHttpServletRequest) req;
            // 从config.json中取得上传文件的ID
            file = mReq.getFile("upfile");

            if (!file.isEmpty()) {
                // 取得文件的原始文件名称
                fileName = file.getOriginalFilename();
                originalFileName = fileName;

                String ext = (FilenameUtils.getExtension(file
                        .getOriginalFilename())).toLowerCase();
                String storePath = "";
                if ("jpg".equals(ext) || "png".equals(ext)
                        || "jpeg".equals(ext) || "bmp".equals(ext)) {
                    storePath = "c:/upload/";
                } else {
                    storePath = "upload/video/";
                }
                System.out.println(storePath + "----storePath");
                // 将图片和视频保存在本地服务器
                String pathRoot = req.getSession().getServletContext().getRealPath("/");
//                String path = pathRoot + "/" + storePath;

                File dest = new File(storePath + fileName);
                System.out.println("临时上传的路径为" + dest.getParentFile());
                if (!dest.getPath().isEmpty()) {
                    file.transferTo(dest);
                    logger.info("target path:" + dest);
                    String doMain = "http://127.0.0.1:8080/";
                    String httpImgPath = doMain + "upload/" + fileName;
                    logger.info("图片上传成功,上传路径为: " + httpImgPath);
                    ueditor.setState("SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容
                    ueditor.setUrl(httpImgPath); // 能访问到你现在图片的路径
                    ueditor.setTitle(originalFileName);
                    ueditor.setOriginal(originalFileName);
                }
                else{
                    logger.info("图片已经存在");
                    ueditor.setState("图片已经存在");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("图片上传失败，失败原因:" + e.getMessage());
            ueditor.setState("FAIL");
        }
        return  ueditor;
    }
//    @RequestMapping(value="/imgUpload")
//    @ResponseBody
//    public Ueditor imgUpload(HttpServletRequest request) throws IOException {
//        Ueditor ueditor = new Ueditor();
//        List<MultipartFile> filesets = ((MultipartHttpServletRequest) request).getFiles("upfile");
//        for(MultipartFile file : filesets){
//            try{
//                String tempfile = file.getOriginalFilename();
//                logger.info("接收到上传数据，图片名称为 ：" + tempfile);
//
//            } catch (IOException e){
//                e.printStackTrace();
//                logger.error("图片上传失败，失败原因:" + e.getMessage());
//                ueditor.setState("FAIL");
//            }
//        }
//
//
//        return ueditor;
//    }
}